home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 1 / Gold Medal Software Volume 1 (Gold Medal) (1994).iso / prog / tpwprog6.arj / LIST.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-07-02  |  4.3 KB  |  162 lines

  1. { list.pas -- Demonstrate list-box control }
  2.  
  3. program List;
  4.  
  5. {$R list.res}
  6.  
  7. uses WinTypes, WinProcs, WObjects, Strings;
  8.  
  9. const
  10.  
  11.   id_Menu    = 100;  { Menu resource ID }
  12.   cm_Dialog  = 101;  { Dialog-command ID }
  13.   id_Dialog  = 200;  { Dialog resource ID }
  14.  
  15.   id_Add     = 101;  { Dialog control ID values }
  16.   id_Delete  = 102;
  17.   id_Input   = 103;
  18.   id_Listbox = 104;
  19.  
  20.   inputLen   = 40;   { Maximum length of input edit control }
  21.  
  22. type
  23.  
  24.   PListDialog = ^ListDialog;
  25.   ListDialog = object(TDialog)
  26.     SelPointer: PChar;
  27.     constructor Init(AParent: PWindowsObject; AName: PChar; P: PChar);
  28.     procedure SetupWindow; virtual;
  29.     procedure IDAdd(var Msg: TMessage);
  30.       virtual id_First + id_Add;
  31.     procedure IDDelete(var Msg: TMessage);
  32.       virtual id_First + id_Delete;
  33.     procedure Ok(var Msg: TMessage);
  34.       virtual id_First + id_Ok;
  35.   end;
  36.  
  37.   ListApplication = object(TApplication)
  38.     procedure InitMainWindow; virtual;
  39.   end;
  40.  
  41.   PListWindow = ^ListWindow;
  42.   ListWindow = object(TWindow)
  43.     Selection: array[0 .. inputLen] of Char;
  44.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  45.     procedure CMDialog(var Msg: TMessage);
  46.       virtual cm_First + cm_Dialog;
  47.   end;
  48.  
  49. {- Add to list box identified by CrtlID in dialog window with the
  50. handle HDlg a new string addressed by P }
  51. procedure SetList(HDlg: HWnd; CtrlID: Word; P: PChar);
  52. begin
  53.   SendDlgItemMessage(HDlg, CtrlID, lb_AddString, 0, LongInt(P))
  54. end;
  55.  
  56.  
  57. { ListDialog }
  58.  
  59. {- Initialize ListDialog instance }
  60. constructor ListDialog.Init(AParent: PWindowsObject; AName: PChar;
  61.   P: PChar);
  62. begin
  63.   TDialog.Init(AParent, AName);
  64.   SelPointer := P  { Save pointer to caller's char array }
  65. end;
  66.  
  67. {- Prepare ListDialog window. Insert list strings. }
  68. procedure ListDialog.SetupWindow;
  69. begin
  70.   TDialog.SetupWindow;
  71.   SendDlgItemMsg(id_Input, em_LimitText, inputLen, 0);
  72.   SetList(HWindow, id_Listbox, 'Apples');
  73.   SetList(HWindow, id_Listbox, 'Peaches');
  74.   SetList(HWindow, id_Listbox, 'Pumpkin');
  75.   SetList(HWindow, id_Listbox, 'Pie');
  76. end;
  77.  
  78. {- Respond to Add button--Add text to list box }
  79. procedure ListDialog.IDAdd(var Msg: TMessage);
  80. var
  81.   Buffer: array[0 .. inputLen] of Char;
  82. begin
  83.   Buffer[0] := Chr(0);
  84.   SendDlgItemMessage(HWindow, id_Input, wm_GetText, inputLen,
  85.     LongInt(@Buffer));
  86.   if StrLen(Buffer) > 0 then
  87.   begin
  88.     SetList(HWindow, id_Listbox, Buffer);
  89.     SendDlgItemMessage(HWindow, id_Input, em_SetSel, 0,
  90.       MakeLong(32767, 0))
  91.   end
  92. end;
  93.  
  94. {- Respond to Delete button--Delete selected list item }
  95. procedure ListDialog.IDDelete(var Msg: TMessage);
  96. var
  97.   Item: Word;   { Selected listbox-item index }
  98. begin
  99.   Item := SendDlgItemMessage(HWindow, id_Listbox, lb_GetCurSel, 0, 0);
  100.   if Item <> lb_Err then
  101.     SendDlgItemMessage(HWindow, id_Listbox, lb_DeleteString, Item, 0)
  102. end;
  103.  
  104. {- Respond to Ok button; Pass selected item back to caller. }
  105. procedure ListDialog.Ok(var Msg: TMessage);
  106. var
  107.   Item: Word;   { Selected listbox-item index }
  108. begin
  109.   Item := SendDlgItemMessage(HWindow, id_Listbox, lb_GetCurSel, 0, 0);
  110.   if Item <> lb_Err then
  111.     SendDlgItemMessage(HWindow, id_Listbox, lb_GetText, Item,
  112.       LongInt(SelPointer))
  113.   else
  114.     SelPointer[0] := Chr(0);  { No selection }
  115.   TDialog.Ok(Msg);
  116. end;
  117.  
  118.  
  119. { ListApplication }
  120.  
  121. {- Initialize ListApplication object's window }
  122. procedure ListApplication.InitMainWindow;
  123. begin
  124.   MainWindow := New(PListWindow, Init(nil, 'List'))
  125. end;
  126.  
  127.  
  128. { ListWindow }
  129.  
  130. {- Construct ListWindow object }
  131. constructor ListWindow.Init(AParent: PWindowsObject; ATitle: PChar);
  132. begin
  133.   TWindow.Init(AParent, ATitle);
  134.   Attr.Menu := LoadMenu(HInstance, PChar(id_Menu));
  135.   Selection[0] := Chr(0)
  136. end;
  137.  
  138. {- Execute Menu:Dialog command }
  139. procedure ListWindow.CMDialog(var Msg: TMessage);
  140. begin
  141.   Application^.ExecDialog(New(PListDialog,
  142.     Init(@Self, PChar(id_Dialog), Selection)));
  143.   if StrLen(Selection) <> 0 then
  144.     MessageBox(HWindow, Selection, 'Selected Item', mb_Ok)
  145. end;
  146.  
  147. var
  148.  
  149.   ListApp: ListApplication;
  150.  
  151. begin
  152.   ListApp.Init('ListApp');
  153.   ListApp.Run;
  154.   ListApp.Done
  155. end.
  156.  
  157.  
  158. {--------------------------------------------------------------
  159.   Copyright (c) 1991 by Tom Swan. All rights reserved.
  160.   Revision 1.00    Date: 3/20/1991
  161. ---------------------------------------------------------------}
  162.